/*
 * [userList]
 * register
 * login
 */
var crypto = require('crypto');
var md5 = function(data) {
    return crypto.createHash('md5').update(data).digest('hex').toUpperCase();
};
var DEFAULT_AVATAR = '//oss.imconfig.com/lib/M.jpg'; //默认头像

module.exports = function(client) {
    console.log('initialization sql-user-object');

    //登录
    this.login = function(account, password, callback) {
        if(sql_check(account)){
            return callback(ResCode.getCode(1006));
        }
        client.query('SELECT \
            id as uid, account, userName, avatar, lastLoginTime, telphone, qq, message \
            FROM ms_user \
            WHERE account=:account AND password=:password', {
            account: account,
            password: md5(password)
        }, function(err, res) {
            if (err) {
                console.log(err);
                return callback(ResCode.getCode(1000));;
            }

            //登录失败
            if (!(res.length > 0)) {
                return callback(ResCode.getCode(1001));
            }
            res[0].lastLoginTime = updateLastLoginTime(res[0].uid);
            res[0].telphone&&(res[0].telphone = res[0].telphone.substr(0, 7) + '****');
            //登录成功
            return callback(ResCode.getCode(0, res[0]));
        });
    };

    //注册
    this.register = function(account, userName, password, callback) {
        if(sql_check(account) || sql_check(userName)){
            return callback(ResCode.getCode(1006));
        }
        client.query('SELECT id \
            FROM ms_user \
            WHERE account = :account', {
            account: account
        }, function(err, res) {
            if (err) {
                console.log(err);
                return callback(ResCode.getCode(1000));
            }

            //账号已存在
            if (res.length > 0) {
                return callback(ResCode.getCode(1002));
            }
            var time = Date.now();
            //继续
            client.query('INSERT INTO ms_user \
                SET account=:account, userName=:userName, password=:password, avatar=:avatar, lastLoginTime=:lastLoginTime, createTime=:createTime ', {
                account: account,
                userName: userName,
                lastLoginTime: time,
                createTime: time,
                password: md5(password),
                avatar: DEFAULT_AVATAR
            }, function(err, res) {
                if (err) {
                    return callback(ResCode.getCode(1000));
                }

                //登录成功
                getUserDataById(res.insertId, callback);
            });
        });
    };

    //第三方登录
    this.thirdRegister = function(userName, openid, avatar, type, callback) {
        client.query('SELECT \
            id as uid, userName, avatar \
            FROM ms_user \
            WHERE token=:token AND type=:type', {
            token: openid,
            type: type
        }, function(err, res) {
            if (err) {
                console.log(err);
                return callback(ResCode.getCode(1000));
            }
            var revertData = {};
            //账号已存在
            if (res.length > 0) {
                return callback(ResCode.getCode(0, res[0]));
            }
            //账号不存在
            client.query('INSERT INTO ms_user \
                SET userName=:userName, password=:password, avatar=:avatar, token=:token, type=:type', {
                userName: userName,
                password: md5(openid),
                lastLoginTime: Data.now(),
                token: openid,
                type: type,
                avatar: avatar
            }, function(err, res) {
                if (err) {
                    console.log(err);
                    return callback(ResCode.getCode(1000));
                }

                //注册成功
                getUserDataById(res.insertId, callback);
            });
        });
    };

    //更新信息
    this.update = function(data, callback) {
        if(sql_check(data)){
            return callback(ResCode.getCode(1006));
        }
        client.query('SELECT \
            id, userName, password, avatar, avatar, telphone, qq, message \
            FROM ms_user \
            WHERE id=:uid', {
            uid: data.uid
        }, function(err, res) {
            if (err) {
                console.log(err);
                return callback(ResCode.getCode(1000));
            }
            //账号不存在
            if (res.length === 0) {
                return callback(ResCode.getCode(1003));
            }
            var resData = res[0];
            for(var i in resData){
                if(data[i]){
                    resData[i] = data[i];
                }
            }
            //账号存在
            client.query('UPDATE ms_user \
                SET userName=:userName, password=:password, avatar=:avatar, telphone=:telphone, qq=:qq, message=:message \
                WHERE id=:id',
                resData,
                function(err, res) {
                if (err) {
                    console.log(err);
                    return callback(ResCode.getCode(1000));
                }

                //修改成功
                getUserDataById(resData.id, callback);
            });
        });
    };

    //id 取，用户信息
    function getUserDataById(id, callback) {
        if(sql_check(id)){
            return callback(ResCode.getCode(1006));
        }
        client.query('SELECT \
            id as uid, account, userName, avatar, lastLoginTime, telphone, qq, message \
            FROM ms_user \
            WHERE id=:id', {
            id: id
        }, function(err, res) {
            if (err) {
                console.log(err);
                return callback(ResCode.getCode(1000));
            }

            if (res.length === 0) {
                return callback(ResCode.getCode(1005));
            }

            res[0].telphone&&(res[0].telphone = res[0].telphone.substr(0, 7) + '****');
            return callback(ResCode.getCode(0, res[0]));
        });
    }

    function updateLastLoginTime(id){
        var time = Date.now();
        client.query('UPDATE ms_user SET lastLoginTime=:lastLoginTime WHERE id=:id', {
            lastLoginTime: time,
            id: id
        }, function(err, res){
            if(err){
                console.log(err);
            }
        });
        return time;
    }

    return this;
};